package com.cy2;

import java.util.Arrays;

/**
 * 简单实现MyArrayList
 *
 * @author CY
 * @date 2022/10/11 02:52
 **/
public class MyArraylist {
    //默认容量
    private static final int DEFAULT_SIZE = 10;
    public int[] elem;
    public int usedSize;//0

    public MyArraylist() {
        this.elem = new int[DEFAULT_SIZE];
    }

    /**
     * 打印顺序表:
     * 根据usedSize判断即可
     */
    public void display() {
        if (this.elem == null) {
            return;
        }
        System.out.print("{");
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i]);
            if (i != this.usedSize - 1) {
                System.out.print(",");
            }
        }
        System.out.print("}");
    }

    public void add(int data) {
        if (this.elem == null) {
            return;
        }
        //判断容量
        if (isFull()) {
            //扩容
            this.elem = Arrays.copyOf(this.elem, 2 * usedSize);
        }
        //添加
        this.elem[usedSize++] = data;
    }

    /**
     * 判断当前的顺序表是不是满的！
     *
     * @return true:满   false代表空
     */
    public boolean isFull() {
        return usedSize == this.elem.length;

    }

    private boolean checkPosInAdd(int pos) {
        if (isFull()) {
            this.elem = Arrays.copyOf(this.elem, 2 * usedSize);
        }
        if (pos >= this.usedSize || pos < 0) {
            throw new PosWrongFulException("pos位置不合法！");

        } else {
            return true;
        }
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        if (checkPosInAdd(pos)) {
            for (int i = this.usedSize - 1; i >= pos; i--) {
                this.elem[i + 1] = this.elem[i];
            }
            this.elem[pos] = data;
        }
        usedSize++;

    }

    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        if (this.elem == null) {
            return false;
        }
        for (int i = 0; i < this.usedSize; i++) {
            if (toFind == this.elem[i]) {
                return true;
            }
        }

        return false;
    }

    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        if (this.usedSize == 0) {
            throw new EmptyException("顺序表为空异常！");
        }
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    // 获取 pos 位置的元素
    public int get(int pos) {
        if (this.usedSize == 0) {
            throw new EmptyException("顺序表为空异常！");
        }
        if (!checkPosInAdd(pos)) {
            throw new PosWrongFulException("pos位置不合法！");
        }
        return this.elem[pos];
    }


    // 给 pos 位置的元素设为【更新为】 value
    public void set(int pos, int value) {
        if (checkPosInAdd(pos)) {
            this.elem[pos] = value;
        }
    }

    /**
     * 删除第一次出现的关键字key
     *
     * @param key
     */
    public int remove(int key) {
        if (this.usedSize == 0) {
            throw new EmptyException("顺序表为空异常！");
        }
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == key) {
                for (int j = i + 1; j <= this.usedSize; j++) {
                    this.elem[j - 1] = this.elem[j];
                }
                this.usedSize--;
                return i;
            }

        }
        return -1;
    }

    // 获取顺序表长度
    public int size() {
        if (this.usedSize == 0) {
            throw new EmptyException("顺序表为空异常！");
        }
        return this.usedSize;
    }

    // 清空顺序表
    public void clear() {
        this.usedSize = 0;
    }
}
